<?php

/**
 * 项目配置管理
 * @copyright   Copyright (c) 2006 - 2010 DZH 友拓软件友拓通
 * @category   	项目配置
 * @package  	Model
 * @author    	黄明兴
 * @version 	2.1,2012-07-22
*/

class AdminPublicModel extends Model {
	// 指定表名
	protected $tableName = 'config';
	
	protected $_validate	 =	 array(
			//array("price_length",'require',"require",1), 	
			array("price_length",'z_integer',"z_integer",2), 	
			array("money_length",'z_integer',"z_integer",2), 	
			array("cube_length",'z_integer',"z_integer",2), 	
			array("add_max_row",'pst_integer',"pst_integer",2), 	
			array("line_number",'pst_integer',"pst_integer",2), 	
			array("fit_max_row",'z_integer',"z_integer",2), 	
			array("fit_max_cols",'z_integer',"z_integer",2), 	
		);
	
	/**
	 * 保存配置信息
	 *
	 */
	public function saveConfig(){
		$type = $_POST['type'];
    	$type_key = intval($_POST['type_key']);
    	if(empty($type)) halt('无法获取配置类型，请检查type值是否正确。');
    	unset($_POST['type'],$_POST['type_key'],$_POST['__hash__']);
    	$this->where('type=\''.$type.'\'')->delete();
    	foreach ($_POST as $key => $value) {
    		if (is_array($value) && !empty($value)) {
    			$value = implode(',',$value);
    		}
    		$this->add(array('key'=>$key,'value'=>$value,'type'=>$type,'type_key'=>$type_key));
    	}
    	@unlink(RUNTIME_FILE);
    	$this->updateInvoiceNode();
    	$this->updateCofig();
	}
	
	
	public function updateCofig(){
		$list = $this->select();
		$config = array();
		foreach ($list as $key => $value) {
			if ($value['type_key']==1) {
				$config[$value['type']][$value['key']] = $value['value'];
			}else {
				$config[$value['key']] = $value['value'];
			}
		}
		$barcode_list	= $this->getBarcodeList();
		$config			= @array_merge((array)$barcode_list,(array)$config);
		file_put_contents(CONFIG_FILE,'<?php return '.var_export($config,true).';?>');
	}
	
	public function getBarcodeList(){
		$info	= M('barcodeRules')->select();
		foreach ((array)$info as $k=>$value){
			if($value['key']=='barcode_length'){
				$list[$value['type']]['barcode_length']	= $value['value'];
				unset($info[$k]);
			}elseif($value['key']=='is_comma'){
				$list[$value['type']]['is_comma']	= $value['value'];
				unset($info[$k]);
			}elseif($value['key']=='country_no'){
				$list[$value['type']]['country_no']	= $value['value'];
				unset($info[$k]);
			}else {
				$list[$value['type']]['barcode'][$value['key']]			= $value['value'];
				$list[$value['type']]['barcode_exchange'][$value['key']]	= $value['is_exchange'];
				unset($info[$k]);
			}
		}
		return $list;
	}
	
    public function updateInvoiceNode(){
    	foreach($_POST as $key=>$val){
    		switch($key){
    			case 'product_from':
    				if($val==1) $id_0 []	= 196;
    				else $id_1 []	= 196;
    				break;
    			case 'client_from':
    				if($val==1) $id_0 [] = 202;
    				else $id_1 [] = 202;
    				break;
    			case 'factory_from':
    				if($val==1) $id_0 [] = 201;
    				else $id_1 [] = 201;
    				break;
    			case 'company_from':
    				if($val==1) $id_0 [] = 198;
    				else $id_1[] = 198;
    				break;
    			default:
    				break;
    		}
    	}
    	if(!empty($id_0)){
    		$ids = implode(',',$id_0);
    		$where	= 'id in ('.$ids.') or parent_id in ('.$ids.')';
    		M('node')->where($where)->setField('status',0);
    	}
    	if(!empty($id_1)){
    		$ids = implode(',',$id_1);
    		$where	= 'id in ('.$ids.') or parent_id in ('.$ids.')';
    		M('node')->where($where)->setField('status',1);
    	}
    }
}